import { request,requestOther} from '@/utils/request';

import type { pdfConvertRequest, imgConvertRequest, convertResult } from '@/api/model/docModel';

const Api = {
  PdfToWord: '/doc/convert/pdf2word',
  PdfToExcel: '/doc/convert/pdf2excel',
  PdfToImage: '/doc/convert/pdf2img',
  ImageToPdf: '/doc/convert/img2pdf',
  ImageToWord: '/doc/convert/img2word',
  ImageToExcel: '/doc/convert/img2excel',
  GetConvertResult: '/doc/convert/result',
};
//微信配置
export function weixin(url:any) {
	 return requestOther.get<object>({
		 url:'https://wx.rscode.cn/wechat/page/config?url='+url
	});
}
//检测微信用户信息
export function checkLogin()
{
	let step=localStorage.getItem('authStep')
	console.log('authStep=',step)
	if(step)
	{
		if(step==='1')
		{
			wxOauth()
		}
		if(step==="2")
		{
			getWxUserInfo()
			.then(ret=>{
				console.log(ret)
				localStorage.setItem('token',ret.access_token)
				localStorage.setItem('authStep',"3")
			})
			.catch(e=>{
				localStorage.setItem('authStep',"1")
			})
		}
		 
	}
}
//微信用户授权
export function wxOauth(){
	localStorage.setItem('authStep',"2")
	let url='https://'+location.host+'/index.html'
	 if(location.host==='wx.rscode.cn')
	 {
		 url='https://'+location.host+'/zhouyi/index.html'
	 }
	location.href="https://wx.rscode.cn/wechat/mp/user/auth?redirectUrl="+encodeURIComponent(url)
}
//使用code获取微信用户信息
export function getWxUserInfo()
{
	let url=location.href;
	console.log('url ',url)
	var params=getUrlParams(url);
	let code = params.code;
	let state = params.state
	//使用code换取token
	var data = {
		code: code,
		state: state,
		inviteCode: '',
		websocketId: ''
	}
	console.log('post /wechat/oauth')
	//使用code换取用户token
	return requestOther.post({
		url: "https://wx.rscode.cn/wechat/oauth",
		data:data
	})
}

//获取url参数
function getUrlParams(url: string): { [key: string]: string } {
    const allParams: { [key: string]: string } = {};
    const queryIndex = url.indexOf('?');
    const hashIndex = url.indexOf('#');

    // 处理 ? 后面的参数
    if (queryIndex !== -1) {
        const queryEnd = hashIndex !== -1 ? hashIndex : url.length;
        const query = url.slice(queryIndex + 1, queryEnd);
        const queryParams = new URLSearchParams(query);
        queryParams.forEach((value, key) => {
            allParams[key] = value;
        });
    }

    // 处理 # 后面的参数
    if (hashIndex !== -1) {
        const hash = url.slice(hashIndex + 1);
        const hashParams = new URLSearchParams(hash);
        hashParams.forEach((value, key) => {
            allParams[key] = value;
        });
    }

    return allParams;
}
